package com.logic.UsersSMS;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.beans.SMSDetialBean;
import com.factory.db.DBFactory;

public class GetUsersSMS {

	public  List<SMSDetialBean> getSMSList( String userid ){

		Connection conn = null;

		PreparedStatement pst = null;

		ResultSet rs = null;

		conn = DBFactory.getDBConnectionInstance( DBFactory.TYPE_MYSQL ).getDBConnection();


		List<SMSDetialBean> mList = new ArrayList<SMSDetialBean>();

		String sql = "select msg from db.jiankong_data_info_table  where controlled_id= ? and msg != '' ORDER BY id DESC limit 0,200";



		try {
			pst = conn.prepareStatement( sql );
			pst.setString( 1, userid );

			rs = pst.executeQuery();

			while (rs.next()) {

				String msg = rs.getString("msg");

				if( !msg.equals("") || msg != null){

					String[] msgArr = msg.split(";;;;;");

					for( int j = 0; j <msgArr.length; j++ ){

						msgArr[j] = msgArr[j].replace("[", "");
						msgArr[j] = msgArr[j].replace("]", "");

						String[] newArr = msgArr[j].split( "!@#" );

						SMSDetialBean  mBean = new SMSDetialBean();

						mBean.setNumber( newArr[0] );
						mBean.setTime(newArr[1] );
						mBean.setContent( newArr[2] );

						mList.add( mBean );


					}
				}

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{


			if( rs != null ){

				try {
					rs.close();

					rs = null;
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			if( pst != null ){

				try {
					pst.close();

					pst = null;
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}



		}

		return mList;

	}

}
